<?php

class Model_ProductNew extends Model {

    function __construct() {
        $this->Lookup = getModel('Model_Lookup');
    }

    function getList() {
        $param = array();
        $param['field'] = "id,product,member_type,sequence";
        $param['where'] = "status=1";
        $param['order'] = " sequence asc";
        $param['group'] = "";
        $param['limit'] = 99;
        $param['pagesize'] = 99;
        $param['page'] = 1;
        $param['join'] = "";
        $param['on'] = "";
        $data = db_query_hash('hilite_trial_product', $param);
        //return array(1,2,3);
		//sos(var_export($data,1));
        $i = $j = 0;
        $datas = $brands = $trim = $comed_arr = $comming_arr = $over_arr = array();
        $brands_trim = $this->Lookup->getLookup('brand');
        if (!empty($brands_trim['result'])) {
            foreach ($brands_trim['result'] as $k => $v) {
                $brands[$v['id']] = $v['name'];
            }
        }

        $temp = array();
        $sums = 8;
        //$expert_sum = 3;
        if (!empty($data['result'])) {
            foreach ($data['result'] as $k => $v) {
                $_arr = array();
                $_arr['field'] = "id,name,cname,brand,quota,(apply_num+apply_num_offset) as apply_num,(click+click_offset) as click,(likes+likes_offset) as likes,body,remark,cover_image1 as cover,is_trial,member_type,status,apply_starttime,apply_endtime,report_starttime,report_endtime,price_trial,capacity_trial,reports";
                $_arr['where'] = 'id=' . $v['product'];
                $_data = db_query_one('product', $_arr);
                $temp[$v['id']] = $_data;

                if (!empty($_data['result']) && !in_array($_data['result']['id'], $trim)) {
                    $_data['result']['type_name'] = $brands[$_data['result']['brand']];
                    $trim[] = $_data['result']['id'];
                    //正在邀請的數組
                    if (time() >= strtotime($_data['result']['apply_starttime']) && time() <= strtotime($_data['result']['apply_endtime'])) {
                        /**
						if (intval($v['member_type']) == 1) {
                            $comed_arr['trial'][] = $_data['result'];
                        } elseif (intval($v['member_type']) == 5) {
                            $comed_arr['expert'][] = $_data['result'];
                        }**/
						$comed_arr[] = $_data['result'];
                    } elseif (time() < strtotime($_data['result']['apply_starttime'])) {//即將來臨的數組
                        /**
						if (intval($v['member_type']) == 1) {
                          $comming_arr['trial'][] = $_data['result'];
                        } elseif (intval($v['member_type']) == 5) {
						  $comming_arr['expert'][] = $_data['result'];
                        }**/
                        $comming_arr[] = $_data['result'];
                    } elseif (time() > strtotime($_data['result']['report_endtime'])) {//已完結的數組
                        $over_arr[] = $_data['result'];
                    }
                }
            }
        }

        //數組組合整理
        /**
          $comed_try_l = count($comed_arr['trial']);
          $comed_exp_l = count($comed_arr['expert']);

          if ($comed_exp_l < $expert_sum && $comed_try_l > $trial_sum) {
          $trial_sum = $sums - $comed_exp_l;
          } elseif ($comed_exp_l > $expert_sum && $comed_try_l < $trial_sum) {
          $expert_sum = $sums - $comed_try_l;
          } elseif ($comed_exp_l <= $expert_sum && $comed_try_l <= $trial_sum) {
          $expert_sum = $comed_exp_l;
          $trial_sum = $comed_try_l;
          } else {
          $expert_sum = 3;
          $trial_sum = 4;
          }
         * */
        
		/**
		for ($i = 0; $i < $expert_sum; $i++) {
            if (!empty($comed_arr['expert'][$i])) {
                $datas['result'][] = $comed_arr['expert'][$i];
            }
        }

        $num = ($sums - count($datas['result']) > count($comed_arr['trial'])) ? count($comed_arr['trial']) : $sums - count($datas['result']);

        for ($i = 0; $i < $num; $i++) {
            if (!empty($comed_arr['trial'][$i])) {
                $datas['result'][] = $comed_arr['trial'][$i];
            }
        }**/
		for ($i = 0; $i < $sums; $i++) {
            if (!empty($comed_arr[$i])) {
                $datas['result'][] = $comed_arr[$i];
            }
        }



        //仍然不足7，補comming產品
        $datas_l = !empty($datas['result']) ? count($datas['result']) : 0;
        if ($datas_l < $sums) {
            $comming_sum = $datas_l >= $sums ? 0 : $sums - $datas_l;
            $comming_sum = count($comming_arr) >= $comming_sum ? $comming_sum : count($comming_arr);
            for ($i = 0; $i < $comming_sum; $i++) {
                $datas['result'][] = $comming_arr[$i];
            }
        }
        //sos(var_export($datas,1));
        //仍然不足7，補已完結產品
        $datas_l = !empty($datas['result']) ? count($datas['result']) : 0;
        if ($datas_l < $sums) {
            $over_sum = $sums - $datas_l;
            $over_sum = count($over_arr) >= $over_sum ? $over_sum : count($over_arr);
            for ($i = 0; $i < $over_sum; $i++) {
                $datas['result'][] = $over_arr[$i];
            }
        }

        //sos($datas_l.'-'.$comming_sum.'--'.$over_sum.'---------------');
        //echo '<pre>'.var_export($comed_arr['expert'],1).'</pre>';
		//sos(var_export($datas,1));
        return $datas;
    }

}

?>